﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace FirmaTransportowa
{
    public partial class AdminPanelCarView : Form
    {

        public AdminPanelCarView()
        {
            InitializeComponent();
            SqlConnection dbConnection2 = SqlConnectionFactory.getConnection();
        }


        private void adminUserViewMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            AdminPanelUserView adminPanel = new AdminPanelUserView();
            adminPanel.Show();
        }

        private void edytujToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
            AdminPanelWorkerView adminPanelWorkerView = new AdminPanelWorkerView();
            adminPanelWorkerView.Show();
        }

        private void displayCars()
        {
            vehiclesDataGridView.DataSource = Vehicle.getVehicles();
            vehiclesDataGridView.Columns[0].Visible = false;
            vehiclesDataGridView.Columns[1].Visible = false;
            vehiclesDataGridView.Columns[2].HeaderText = "rodzaj samochodu";
            vehiclesDataGridView.Columns[3].HeaderText = "pojemność";
            vehiclesDataGridView.Columns[4].HeaderText = "numer silnika";
            vehiclesDataGridView.Columns[5].HeaderText = "pojemność skokowa";
            vehiclesDataGridView.Columns[6].HeaderText = "marka";
            vehiclesDataGridView.Columns[7].HeaderText = "model";
        }

        private void AdminPanelCarView_Load(object sender, EventArgs e)
        {
            displayCars();
        }

        private void addNewCarButton_Click(object sender, EventArgs e)
        {
            AdminAddNewCar adminAddNewCar = new AdminAddNewCar();
            adminAddNewCar.eventClosed += OnAddNewCarClosed;
            adminAddNewCar.Show();
        }

        private void refreshButton_Click(object sender, EventArgs e)
        {
            displayCars();
        }

        private void deleteCarButton_Click(object sender, EventArgs e)
        {
            Vehicle vehicle = Vehicle.getById(int.Parse(vehiclesDataGridView.Rows[vehiclesDataGridView.CurrentRow.Index].Cells[0].Value.ToString()));
            vehicle.active = false;
            vehicle.save();
            displayCars();
            MessageBox.Show("Skasowano samochód");

        }

        private void editButton_Click(object sender, EventArgs e)
        {
            try
            {
                AdminAddNewCar adminAddNewCar = new AdminAddNewCar(Vehicle.getById(int.Parse(vehiclesDataGridView.Rows[vehiclesDataGridView.CurrentRow.Index].Cells[0].Value.ToString())), true);
                adminAddNewCar.Show();
            }
            catch (NullReferenceException ex)
            {
                MessageBox.Show("Wybierz samochód do edycji!");
            }
        }

        private void OnAddNewCarClosed()
        {
            vehiclesDataGridView.DataSource = null;
            displayCars();
        }

        private void AdminPanelCarView_HelpButtonClicked(object sender, CancelEventArgs e)
        {
            HelpUtils.showHelp(this, 25);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.F1)
            {
                HelpUtils.showHelp(this, 25);
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }
}
